//
//  NetworkTools.swift
//  XiaoJingMallSwift
//
//  Created by beisheng03 on 2017/7/21.
//  Copyright © 2017年 beisheng. All rights reserved.
//

import UIKit
import AFNetworking

enum HTTP_REQUEST_METHOD : Int{
    
    case GET_METHOD = 0
    case POST_METHOD
    case HEAD_METHOD
    case PUT_METHOD
    case PATCH_METHOD
    case DELETE_METHOD
    
}

class NetworkTools: AFHTTPSessionManager {

    static let reachability : Reachability = {
        
        let tamp = Reachability()
        
        return tamp!
        
    }()
    
    
    
    static let shared: NetworkTools = {
        
        let config = URLSessionConfiguration.default
        
        let temp = NetworkTools(baseURL:URL(string: "http://mall.xjscvip.com/") , sessionConfiguration: config)
        
        temp.responseSerializer.acceptableContentTypes = Set(arrayLiteral: "application/json", "text/json", "text/plain", "text/javascript","text/html")
        
        temp.requestSerializer.timeoutInterval = 10
        
        
        
        
        return temp
    }()

}






extension NetworkTools{
    
    typealias FinishedResult = (_ responseObject: [String: AnyObject]?, _ error: NSError?)->()
    
    func sendRequest(method : HTTP_REQUEST_METHOD,pathStr : String,parameters : [String: AnyObject]?,completionHandler : @escaping FinishedResult) {
        
        if !(NetworkTools.reachability.isReachable) {
            
            let error = NSError(domain: "无网络连接!", code: -1001, userInfo: nil)
            
            completionHandler(nil, error);
            
            
            return
        }
        
        
        let successCallback = { (tast :URLSessionDataTask, responseObject : Any?) in
            
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            
            let response = responseObject as! [String : AnyObject]
            
            let codeString = response["code"] as! String
            
            let errorString = response["error"] as? String
            
            let codeValue = Int(codeString)!
            
            
            if codeValue == 400 {
                
                let error = NSError(domain: errorString!, code: 400, userInfo: nil)

                completionHandler(nil, error);

            } else {
                
                completionHandler(response, nil);

            }
            
            
        }
        
        
        let failureCallback = { (tast :URLSessionDataTask?, error : Error) in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            
            
            let error = NSError(domain: "请求异常!", code: -1002, userInfo: nil)
            
            completionHandler(nil, error);
            
        }
        
        
        
//        var parameters  = [String : AnyObject]()
//        


        
        var parmeter = parameters
        
        if addKey == false {
            addKey = true
        }else{
        
        
            if parmeter == nil {
                
                
                if UserViewModel.shared.userIsLogin() {
                    
                    parmeter  = [String : AnyObject]()
                    
                    parmeter!["key"] = UserViewModel.shared.key as AnyObject
                    
                }
                
            } else {
                
                
                
                if UserViewModel.shared.userIsLogin() {
                    
                    parmeter!["key"] = UserViewModel.shared.key as AnyObject
                    
                }
                
            }
            

        
        
        }
        
        
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        
        if method == .GET_METHOD {
            
            get(pathStr, parameters: parmeter, progress: nil, success:successCallback , failure: failureCallback)
            
            
        } else {
            
            post(pathStr, parameters: parmeter, progress: nil, success: successCallback, failure: failureCallback)
            
        }
        
        
        
        
    }
    
    
    
}


